package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("输入要删除的书名");
        Scanner scanner=new Scanner(System.in);
        String Name=scanner.nextLine();
        int currentSize=bookList.getUseSize();
        int i = 0;
        int pos=-1;
        for (; i <currentSize ; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(Name)){
                System.out.println("找到了，开始删除");
                pos=i;
                break;
            }
        }
        if (i>=currentSize) {
            System.out.println("此书没有，删除失败");
            return;
        }
        for (int j = pos; j <currentSize-1 ; j++) {
            Book book1=bookList.getBook(j+1);
            bookList.setBook(j,book1);
            return;
        }
        bookList.setUseSize(currentSize-1);
        bookList.setBook(currentSize-1,null);
        System.out.println("删除成功");
    }
}
